<?php
/**
 * Created by PhpStorm.
 * User : Orange Tel: 181-3414-1353 QQ:1139552211
 * Date    : 2021/6/11 下午3:52
 */

namespace app\index\controller;


use app\common\controller\IndexController;
use app\admin\model\RechargeAddress;
use app\Request;
use app\admin\model\UserTransaction;

/**
 * Class recharge
 * @package app\index\controller
 * User : Orange Tel: 181-3414-1353 QQ:1139552211
 * Date    : 2021/6/11 下午3:52
 */
class Recharge extends IndexController {


    /**
     * 获取所有充币提币状态值
     * User : Orange Tel: 181-3414-1353 QQ:1139552211
     * Date    : 2021/6/11 下午3:56
     */
    public function get_recharge_type() {
        $this->success('成功', RechargeAddress::instance()->getAddressTypeInfo());
    }

    /**
     * 根据TYPE获取充币信息
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * User : Orange Tel: 181-3414-1353 QQ:1139552211
     * Date    : 2021/6/15 上午10:01
     */
    public function get_recharge_info() {
        $input = input();

        $validate = \think\facade\Validate::rule([
            'type' => 'require|number',
        ]);

        $info     = RechargeAddress::instance()->where(['type' => $input['type']])->find();

        $this->success('成功', $info);
    }

    /**
     * 充值
     * @param Request $request
     * User : Orange Tel: 181-3414-1353 QQ:1139552211
     * Date    : 2021/6/11 下午5:09
     */
    public function add_recharge_type(Request $request) {
        $user_id = $request->uid;
        $input   = input();

        $validate = \think\facade\Validate::rule([
            'chongti' => 'require|number',
            'chain'   => 'require|number',
            'number'  => 'require|number',
        ]);

        $data = [
            'user_id'     => $user_id,
            'chongti'     => $input['chongti'],
            'type'        => $input['type'],
            'number'      => $input['number'],
            'status'      => 0,
            'create_time' => time()
        ];

        $id = UserTransaction::instance()->insertGetId($data);

        if (empty($id)) {
            $this->error('充值失败');
        }

        $this->success('充值成功', ['order_id' => $id]);
    }
}